package com.yl.ydk.ui.activity

import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.yl.ydk.R
import com.yl.ydk.base.BaseActivity
import com.yl.ydk.base.BaseFragmentPageAdapter
import com.yl.ydk.bean.TabEntity
import com.yl.ydk.ui.fragment.home.DeviceFragment
import com.yl.ydk.ui.fragment.home.HealthyFragment
import com.yl.ydk.ui.fragment.home.LifeFragment
import com.yl.ydk.ui.fragment.home.SkillFragment
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import kotlinx.android.synthetic.main.activity_home.*
import java.util.*

class HomeActivity : BaseActivity() {

    private var mAdapter: BaseFragmentPageAdapter? = null
    private val mTitleList = ArrayList<String>()
    private val mFragments = ArrayList<Fragment>()

    private val mTitles = arrayOf("体检", "健康", "生活", "技能")
    private val mTabEntities = ArrayList<CustomTabEntity>()

    private val mIconUnselectIds = intArrayOf(
        R.drawable.icon_tab_wodetijianx, R.drawable.icon_tab_jiankang,
        R.drawable.icon_tab_shenghuo, R.drawable.icon_tab_gengduo
    )
    private val mIconSelectIds = intArrayOf(
        R.drawable.icon_tab_wodetijianx_p, R.drawable.icon_tab_jiankang_p,
        R.drawable.icon_tab_shenghuo_p, R.drawable.icon_tab_gengduo
    )

    companion object {
        var homeActivity: HomeActivity? = null
    }

    override fun getLayoutId(): Int = R.layout.activity_home

    override fun attachView() {

    }

    override fun detachView() {

    }

    override fun initView() {

        mTitles.forEachIndexed { i, k ->
            mTabEntities.add(TabEntity(k, mIconSelectIds[i], mIconUnselectIds[i]))
        }

        mFragments.add(DeviceFragment.getInstance())
        mFragments.add(HealthyFragment.getInstance())
        mFragments.add(LifeFragment.getInstance())
        mFragments.add(SkillFragment.getInstance())

        mAdapter = BaseFragmentPageAdapter(supportFragmentManager, mFragments, mTitleList)
        vpPlan?.adapter = mAdapter
        vpPlan.currentItem = 0
        tlPlan.setTabData(mTabEntities)
        tlPlan.setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                vpPlan.currentItem = position
                tlPlan.currentTab = position
            }

            override fun onTabReselect(position: Int) {

            }
        })

        vpPlan.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }

            override fun onPageSelected(position: Int) {
                tlPlan.currentTab = position
            }

            override fun onPageScrollStateChanged(state: Int) {

            }
        })
        homeActivity = this
    }

}